// pages/storeDetail/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    state: 1,
    rate: 3.5,
    active: 1,
    tabOptions: [{
        title: "团购",
        viewId: "tuangou"
      },
      {
        title: "评价",
        viewId: "pingjia"
      },
    ],
    // 标签切换id
    viewId: '',
    id: null,
    store: {},
    page: 1,
    limit: 10,
    plList: [],
    latitude:'',
    longitude:'',
    placeName:'',
    navbg:false
  },


  clickTab(e) {
    this.setData({
      viewId: e.detail.name
    });
  },

  gotoTimeCar(e) {
    const data = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/timeCar/index?data=' + data+'&storeId='+this.data.id+'&storeName='+this.data.store.name,
    })
  },

  userCollect(){
    var userLogin= wx.getStorageSync('userLogin')
    if (!userLogin.mobile) {
      wx.navigateTo({
        url: '/pages/login/index',
      })
      return false
    }

    const {id} = this.data
    
     const userId = wx.getStorageSync('userLogin').userId;
     app.agriknow.userCollect({
      id:parseInt(id),
      userId
     // situation: -75.154214,
     // latitude: 41.525454,
    }).then(res => {
      if(res.code == '000000'){
        this.getStoreDetail()
      }
    })
  },


  getStoreDetail() {
    var userId = null
    var userInfo= wx.getStorageSync('userInfo')
    if (userInfo) {
     userId=userInfo.userId
    }else{
     userId=0
    }
    app.agriknow.getStoreDetail({
      userId,
      id: Number(this.data.id),
      situation: this.data.longitude,
      latitude: this.data.latitude,
      area: "",
      city: "",
      province: "",
    }).then(res => {
      if (res.code == "000000") {
        
        let label = [];
        let allrate = 0;
        let distance = ''
        var datas=res.data
        
        let storeImage = [];
        if(datas.storeImage !== null && datas.storeImage !== ""){
          datas.storeImage = datas.storeImage.split(',');
        }
       
       
  
        if(datas.label !== null && datas.label !== ""){
          datas.label = datas.label.split(',');
        }

        datas.allrate = (Number(datas.serviceFraction) + Number(datas.priceFraction) + Number(datas.ambientFraction))/3
        
        datas.distance=(datas.distance/1000).toFixed(2)

        this.setData({
          //store: {...res.data, label, storeImage,deskImage, allrate,distance}
          store:datas
        })
      }
    })
  },

  getpllist(){
    const { id, page, limit} = this.data
    app.agriknow.getpllist({
      id,
      page,
      limit
    }).then(res => {
      if(res.code == '000000'){
        let rate = 0;
        rate = (Number(res.data.serviceFraction) + Number(res.data.priceFraction) + Number(res.data.ambientFraction))/3
        this.setData({
          plList: res.data.list,
          rate
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options)
    if(options.data){
      console.log(1)
      this.setData({
        id: options.data
      })
    }else{
      console.log(2)
      this.setData({
        id:decodeURIComponent(options.scene).split('=')[1]
      })
    }
    
    this.locationData()
    this.getStoreDetail();
   // this.getpllist();
    
  },
  //获取位置
  locationData() {
    //获取当前位置
    // utils.locationData()
    let locationInfo = wx.getStorageSync('locationInfo')
    if(locationInfo){
      this.setData({
        latitude: locationInfo.location.lat,
        longitude: locationInfo.location.lng,
        placeName: locationInfo.address_component.city,
      })
      console.log(this.data.placeName)
    }
    else{
    }
    
  },
  //导航
  navigationTap() {
    let taht = this
    wx.openLocation({
      latitude: Number(taht.data.store.latitude),	//维度
      longitude: Number(taht.data.store.situation), //经度
      name: taht.data.placeName,	//目的地定位名称
      scale: 15,	//缩放比例
      address: taht.data.store.address	//导航详细地址
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
      return{
        title:this.data.store.name,
        imageUrl:this.data.store.image
      }
  },
  call:function(e){
    var mobile=e.currentTarget.dataset.mobile
    wx.makePhoneCall({
      phoneNumber: mobile,
    })
  },
  onPageScroll: function (e) {

    if (e.scrollTop > 20) {
      this.setData({ 
        navbg:true
      })
    } else {
      this.setData({ 
        navbg:false
      })
    }
  }, 

})